package tech.abing.spider.task.pageprocessor.amac;

import java.sql.Timestamp;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.BinaryRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.constants.TaskConstants;
import tech.abing.spider.task.model.amac.AttachmentEntity;
import tech.abing.spider.task.model.amac.CancelListEntity;
import tech.abing.spider.util.DateUtils;
import tech.abing.spider.util.StringUtils;
import tech.abing.spider.util.UuIdPublic;

public class CancelListDetailProcessor implements PageProcessor {

	private static Logger logger = Logger.getLogger(CancelListDetailProcessor.class);
	
	private static int record = 0;
	
	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		Document doc = Jsoup.parse(page.getContent());
		Element table = doc.select("div.innerCenter > div.iRight > div.ldContent").first();
		Elements trs =table.select("tr:gt(0)");
		for (Element tr : trs) {
			record++;
			logger.info("正在解析第"+record+"条数据:");
			CancelListEntity cl = new CancelListEntity();
			cl.setId(UuIdPublic.timeStrongID(5));
			cl.setCreateTime(new Timestamp(System.currentTimeMillis()));
			String url = page.getRequest().getUrl();
			cl.setUrl(url);
			Elements tds = tr.select("td");
			String cancelDate = tds.get(0).text().replace(Jsoup.parse("&nbsp;").text(),"");
			if(StringUtils.isNotEmpty(cancelDate)){
				cl.setCancelDate(DateUtils.parseDate(cancelDate));
			}
			cl.setOrgName(tds.get(1).text().replace(Jsoup.parse("&nbsp;").text(),"").replace(" ",""));
			cl.setOrgAttachmentUrl(tds.get(1).select("a[filerela]").attr("href"));
			cl.setDisciplinaryName(tds.get(2).text());
			cl.setDisciplinaryAttachmentUrl(tds.get(2).select("a[filerela]").attr("href"));
			if(StringUtils.isNotEmpty(cl.getOrgAttachmentUrl())){
				AttachmentEntity attachment = new AttachmentEntity();
				String uuid = UuIdPublic.timeStrongID(5);
				attachment.setAttachType(TaskConstants.TASK_CANCELLIST_ORG);
				attachment.setUrl(url);
				attachment.setDownloadAddress(cl.getOrgAttachmentUrl());
				attachment.setFileName(uuid);
				attachment.setDisplayName(tds.get(1).select("a[filerela]").text());
				String savePath = "doc/"+DateUtils.getDate("yyyyMMdd")+"/"+attachment.getAttachType()+"/";
				attachment.setSavePath(savePath);
				attachment.setFileType("pdf");
				attachment.setCreateTime(new Timestamp(System.currentTimeMillis()));
				attachment.setId(uuid);
				StringUtils.removeSpace(attachment);
				objectContainer.add(attachment);
				BinaryRequest req = context.createBinaryRequest(cl.getOrgAttachmentUrl(), AttachmentProcessor.class);
				req.addAttribute("attachment", attachment);
				
				queue.add(req);
			}
			if(StringUtils.isNotEmpty(cl.getDisciplinaryAttachmentUrl())){
				AttachmentEntity attachment = new AttachmentEntity();
				String uuid = UuIdPublic.timeStrongID(5);
				attachment.setUrl(url);
				attachment.setAttachType(TaskConstants.TASK_CALCELLIST_DISCIPLINARY);
				attachment.setDownloadAddress(cl.getDisciplinaryAttachmentUrl());
				attachment.setFileName(uuid);
				attachment.setDisplayName(tds.get(2).select("a[filerela]").text());
				String savePath = "doc/"+DateUtils.getDate("yyyyMMdd")+"/"+attachment.getAttachType()+"/";
				attachment.setSavePath(savePath);
				attachment.setFileType("pdf");
				attachment.setCreateTime(new Timestamp(System.currentTimeMillis()));
				attachment.setId(uuid);
				StringUtils.removeSpace(attachment);
				objectContainer.add(attachment);
				BinaryRequest req = context.createBinaryRequest(cl.getDisciplinaryAttachmentUrl(), AttachmentProcessor.class);
				req.addAttribute("attachment", attachment);
				
				queue.add(req);
			}
			StringUtils.removeSpace(cl);
			objectContainer.add(cl);
		}
		
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}


}
